//
//  StarParticle.m
//  StarBugs
//
//  Created by ?? ? on 2/28/12.
//  Copyright 2012 antisword@playorca.com. All rights reserved.
//

#include "Sprite/StarParticle.h"
#include "Util/ImageManager.h"
#include "Util/ModuleController.h"

#define MAX_TIME 3
using namespace cocos2d;

StarParticle::~StarParticle()
{
    
}

StarParticle::StarParticle(CCPoint pt)
{

    mainSprite = new CCSprite();
    mainSprite->init();
    mainSprite->autorelease();
    addChild(mainSprite);
    for(int i = 0; i < MAX_PARTICLE; i++)
    {
        particle[i] = CCSprite::create("particle2.png");
        particle[i]->setPosition(pt);
        mainSprite->addChild(particle[i]);
        speed[i].x = CCRANDOM_MINUS1_1() * 10 *ModuleController::getImageScale();
        speed[i].y = CCRANDOM_0_1() * 20 *ModuleController::getImageScale();
        particle[i]->setScale(CCRANDOM_0_1() * 0.5 + 0.5);
    }
    timer = 0;
    schedule(schedule_selector(StarParticle::tick));
}

void StarParticle::tick(float dt)
{
    timer += dt;
    for(int i = 0; i < MAX_PARTICLE; i++)
    {
        speed[i].y = speed[i].y -= 20 * dt *ModuleController::getImageScale();
        CCPoint pt = particle[i]->getPosition();
        pt.x += speed[i].x * dt * 10 *ModuleController::getImageScale();
        pt.y += speed[i].y * dt * 10 *ModuleController::getImageScale();
        
        particle[i]->setPosition(pt);
        particle[i]->setRotation(timer * 360);
        particle[i]->setOpacity(255 - 255 * timer / MAX_TIME);

    }
    
    if(timer  > MAX_TIME)
    {
        removeFromParentAndCleanup(true);
    }
}

#pragma mark particle2
#define MAX_TIME2   2.0
StarParticle2::~StarParticle2()
{
    
}

StarParticle2::StarParticle2(CCPoint pt)
{
    mainSprite = new CCSprite();
    mainSprite->init();
    mainSprite->autorelease();
    addChild(mainSprite);
    for(int i = 0; i < MAX_PARTICLE2; i++)
    {
        if(i % 3 == 0)
            particle[i] = ImageManager::getSpriteWithXml2(kImageSheet_effect_clear2, "star1");
        else if(i % 3 == 1)
            particle[i] = ImageManager::getSpriteWithXml2(kImageSheet_effect_clear2, "star2");
        else
            particle[i] = ImageManager::getSpriteWithXml2(kImageSheet_effect_clear2, "star3");
        
        //particle[i]->setBlendFunc((ccBlendFunc){GL_SRC_ALPHA, GL_ONE});
		ccBlendFunc cf = {GL_SRC_ALPHA, GL_ONE};
		particle[i]->setBlendFunc(cf);
        particle[i]->setPosition(pt);
        particle[i]->setScale(CCRANDOM_0_1() * 0.7 + 0.2);
        mainSprite->addChild(particle[i]);
        speed[i].x = CCRANDOM_MINUS1_1() * 20 *ModuleController::getImageScale();
        speed[i].y = CCRANDOM_MINUS1_1() * 20 *ModuleController::getImageScale();
//        particle[i]->setScale(CCRANDOM_0_1() * 0.5 + 0.5);
    }
    timer = 0;
    schedule(schedule_selector(StarParticle2::tick));
}

void StarParticle2::tick(float dt)
{
    timer += dt;
    for(int i = 0; i < MAX_PARTICLE2; i++)
    {
        speed[i].x = speed[i].x - (speed[i].x * ((0.2 * dt) / 1000 *ModuleController::getImageScale()));
        speed[i].y = speed[i].y - (speed[i].y * ((0.2 * dt) / 1000 *ModuleController::getImageScale()));
        CCPoint pt = particle[i]->getPosition();
        pt.x += speed[i].x * dt * 10 *ModuleController::getImageScale();
        pt.y += speed[i].y * dt * 10 *ModuleController::getImageScale();
        
        particle[i]->setPosition(pt);
        particle[i]->setRotation(timer * 90);
        if(timer > MAX_TIME2 - 0.3)
        {
            particle[i]->setScale(particle[i]->getScale() * (MAX_TIME2 - timer) / 0.3);
            particle[i]->setOpacity(255 * (MAX_TIME2 - timer) / 0.3);
        }
        
    }
    
    if(timer  > MAX_TIME2)
    {
        removeFromParentAndCleanup(true);
    }
}
